
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>可选链 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="17_Error_Handling.html" />
    
    
    <link rel="prev" href="15_Deinitialization.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >可选链</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;">&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;</h1>
<p><em>&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;</em>&#x662F;&#x4E00;&#x79CD;&#x53EF;&#x4EE5;&#x5728;&#x5F53;&#x524D;&#x503C;&#x53EF;&#x80FD;&#x4E3A; <code>nil</code> &#x7684;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x8BF7;&#x6C42;&#x548C;&#x8C03;&#x7528;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x53CA;&#x4E0B;&#x6807;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x53EF;&#x9009;&#x503C;&#x6709;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x8C03;&#x7528;&#x5C31;&#x4F1A;&#x6210;&#x529F;&#xFF1B;&#x5982;&#x679C;&#x53EF;&#x9009;&#x503C;&#x662F; <code>nil</code>&#xFF0C;&#x90A3;&#x4E48;&#x8C03;&#x7528;&#x5C06;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;&#x591A;&#x4E2A;&#x8C03;&#x7528;&#x53EF;&#x4EE5;&#x8FDE;&#x63A5;&#x5728;&#x4E00;&#x8D77;&#x5F62;&#x6210;&#x4E00;&#x4E2A;&#x8C03;&#x7528;&#x94FE;&#xFF0C;&#x5982;&#x679C;&#x5176;&#x4E2D;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x4E3A; <code>nil</code>&#xFF0C;&#x6574;&#x4E2A;&#x8C03;&#x7528;&#x94FE;&#x90FD;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x5373;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>Swift &#x7684;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x548C; Objective-C &#x4E2D;&#x5411; <code>nil</code> &#x53D1;&#x9001;&#x6D88;&#x606F;&#x6709;&#x4E9B;&#x76F8;&#x50CF;&#xFF0C;&#x4F46;&#x662F; Swift &#x7684;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x53EF;&#x4EE5;&#x5E94;&#x7528;&#x4E8E;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x4E14;&#x80FD;&#x68C0;&#x67E5;&#x8C03;&#x7528;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</p>
</blockquote>
<h2 id="optional-chaining-as-an-alternative-to-forced-unwrapping">&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4EE3;&#x66FF;&#x5F3A;&#x5236;&#x5C55;&#x5F00; </h2>
<p>&#x901A;&#x8FC7;&#x5728;&#x60F3;&#x8C03;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#xFF0C;&#x6216;&#x4E0B;&#x6807;&#x7684;&#x53EF;&#x9009;&#x503C;&#x540E;&#x9762;&#x653E;&#x4E00;&#x4E2A;&#x95EE;&#x53F7;&#xFF08;<code>?</code>&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x94FE;&#x3002;&#x8FD9;&#x4E00;&#x70B9;&#x5F88;&#x50CF;&#x5728;&#x53EF;&#x9009;&#x503C;&#x540E;&#x9762;&#x653E;&#x4E00;&#x4E2A;&#x53F9;&#x53F7;&#xFF08;<code>!</code>&#xFF09;&#x6765;&#x5F3A;&#x5236;&#x5C55;&#x5F00;&#x5B83;&#x7684;&#x503C;&#x3002;&#x5B83;&#x4EEC;&#x7684;&#x4E3B;&#x8981;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x5F53;&#x53EF;&#x9009;&#x503C;&#x4E3A;&#x7A7A;&#x65F6;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x53EA;&#x4F1A;&#x8C03;&#x7528;&#x5931;&#x8D25;&#xFF0C;&#x7136;&#x800C;&#x5F3A;&#x5236;&#x5C55;&#x5F00;&#x5C06;&#x4F1A;&#x89E6;&#x53D1;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x53CD;&#x6620;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x53EF;&#x4EE5;&#x5728;&#x7A7A;&#x503C;&#xFF08;<code>nil</code>&#xFF09;&#x4E0A;&#x8C03;&#x7528;&#x7684;&#x4E8B;&#x5B9E;&#xFF0C;&#x4E0D;&#x8BBA;&#x8FD9;&#x4E2A;&#x8C03;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x53CA;&#x4E0B;&#x6807;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x662F;&#x4E0D;&#x662F;&#x53EF;&#x9009;&#x503C;&#xFF0C;&#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x8FD9;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;&#x6765;&#x5224;&#x65AD;&#x4F60;&#x7684;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x662F;&#x5426;&#x8C03;&#x7528;&#x6210;&#x529F;&#xFF0C;&#x5982;&#x679C;&#x8C03;&#x7528;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x5219;&#x8BF4;&#x660E;&#x8C03;&#x7528;&#x6210;&#x529F;&#xFF0C;&#x8FD4;&#x56DE; <code>nil</code> &#x5219;&#x8BF4;&#x660E;&#x8C03;&#x7528;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x7279;&#x522B;&#x6307;&#x51FA;&#xFF0C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4E0E;&#x539F;&#x672C;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#x88AB;&#x5305;&#x88C5;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;&#xFF0C;&#x5F53;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6210;&#x529F;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x5C5E;&#x6027;&#x539F;&#x672C;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x662F; <code>Int</code> &#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x4F1A;&#x53D8;&#x4E3A; <code>Int?</code> &#x7C7B;&#x578B;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x51E0;&#x6BB5;&#x4EE3;&#x7801;&#x5C06;&#x89E3;&#x91CA;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x548C;&#x5F3A;&#x5236;&#x5C55;&#x5F00;&#x7684;&#x4E0D;&#x540C;&#x3002;</p>
<p>&#x9996;&#x5148;&#x5B9A;&#x4E49;&#x4E24;&#x4E2A;&#x7C7B; <code>Person</code> &#x548C; <code>Residence</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">var</span> residence: <span class="hljs-type">Residence</span>?
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Residence</span> </span>{
    <span class="hljs-keyword">var</span> numberOfRooms = <span class="hljs-number">1</span>
}
</code></pre>
<p><code>Residence</code> &#x6709;&#x4E00;&#x4E2A; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x5C5E;&#x6027; <code>numberOfRooms</code>&#xFF0C;&#x5176;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A; <code>1</code>&#x3002;<code>Person</code> &#x5177;&#x6709;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684; <code>residence</code> &#x5C5E;&#x6027;&#xFF0C;&#x5176;&#x7C7B;&#x578B;&#x4E3A; <code>Residence?</code>&#x3002;</p>
<p>&#x5047;&#x5982;&#x4F60;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684; <code>Person</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x5B83;&#x7684; <code>residence</code> &#x5C5E;&#x6027;&#x7531;&#x4E8E;&#x662F;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x800C;&#x5C06;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x4E3A; <code>nil</code>&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>john</code> &#x6709;&#x4E00;&#x4E2A;&#x503C;&#x4E3A; <code>nil</code> &#x7684; <code>residence</code> &#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> john = <span class="hljs-type">Person</span>()
</code></pre>
<p>&#x5982;&#x679C;&#x4F7F;&#x7528;&#x53F9;&#x53F7;&#xFF08;<code>!</code>&#xFF09;&#x5F3A;&#x5236;&#x5C55;&#x5F00;&#x83B7;&#x5F97;&#x8FD9;&#x4E2A; <code>john</code> &#x7684; <code>residence</code> &#x5C5E;&#x6027;&#x4E2D;&#x7684; <code>numberOfRooms</code> &#x503C;&#xFF0C;&#x4F1A;&#x89E6;&#x53D1;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x65F6; <code>residence</code> &#x6CA1;&#x6709;&#x53EF;&#x4EE5;&#x5C55;&#x5F00;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> roomCount = john.residence!.numberOfRooms
<span class="hljs-comment">// &#x8FD9;&#x4F1A;&#x5F15;&#x53D1;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;</span>
</code></pre>
<p><code>john.residence</code> &#x4E3A;&#x975E; <code>nil</code> &#x503C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x8C03;&#x7528;&#x4F1A;&#x6210;&#x529F;&#xFF0C;&#x5E76;&#x4E14;&#x628A; <code>roomCount</code> &#x8BBE;&#x7F6E;&#x4E3A; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x623F;&#x95F4;&#x6570;&#x91CF;&#x3002;&#x6B63;&#x5982;&#x4E0A;&#x9762;&#x63D0;&#x5230;&#x7684;&#xFF0C;&#x5F53; <code>residence</code> &#x4E3A; <code>nil</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0A;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4F1A;&#x89E6;&#x53D1;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x4E00;&#x79CD;&#x8BBF;&#x95EE; <code>numberOfRooms</code> &#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4F7F;&#x7528;&#x95EE;&#x53F7;&#xFF08;<code>?</code>&#xFF09;&#x6765;&#x66FF;&#x4EE3;&#x539F;&#x6765;&#x7684;&#x53F9;&#x53F7;&#xFF08;<code>!</code>&#xFF09;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> roomCount = john.residence?.numberOfRooms {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s residence has <span class="hljs-subst">\(roomCount)</span> room(s).&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the number of rooms.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Unable to retrieve the number of rooms.&#x201D;</span>
</code></pre>
<p>&#x5728; <code>residence</code> &#x540E;&#x9762;&#x6DFB;&#x52A0;&#x95EE;&#x53F7;&#x4E4B;&#x540E;&#xFF0C;Swift &#x5C31;&#x4F1A;&#x5728; <code>residence</code> &#x4E0D;&#x4E3A; <code>nil</code> &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x8BBF;&#x95EE; <code>numberOfRooms</code>&#x3002;</p>
<p>&#x56E0;&#x4E3A;&#x8BBF;&#x95EE; <code>numberOfRooms</code> &#x6709;&#x53EF;&#x80FD;&#x5931;&#x8D25;&#xFF0C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4F1A;&#x8FD4;&#x56DE; <code>Int?</code> &#x7C7B;&#x578B;&#xFF0C;&#x6216;&#x79F0;&#x4E3A;&#x201C;&#x53EF;&#x9009;&#x7684; <code>Int</code>&#x201D;&#x3002;&#x5982;&#x4E0A;&#x4F8B;&#x6240;&#x793A;&#xFF0C;&#x5F53; <code>residence</code> &#x4E3A; <code>nil</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x9009;&#x7684; <code>Int</code> &#x5C06;&#x4F1A;&#x4E3A; <code>nil</code>&#xFF0C;&#x8868;&#x660E;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE; <code>numberOfRooms</code>&#x3002;&#x8BBF;&#x95EE;&#x6210;&#x529F;&#x65F6;&#xFF0C;&#x53EF;&#x9009;&#x7684; <code>Int</code> &#x503C;&#x4F1A;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x7ED1;&#x5B9A;&#x5C55;&#x5F00;&#xFF0C;&#x5E76;&#x8D4B;&#x503C;&#x7ED9;&#x975E;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684; <code>roomCount</code> &#x5E38;&#x91CF;&#x3002;</p>
<p>&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5373;&#x4F7F; <code>numberOfRooms</code> &#x662F;&#x975E;&#x53EF;&#x9009;&#x7684; <code>Int</code> &#x65F6;&#xFF0C;&#x8FD9;&#x4E00;&#x70B9;&#x4E5F;&#x6210;&#x7ACB;&#x3002;&#x53EA;&#x8981;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5C31;&#x610F;&#x5473;&#x7740; <code>numberOfRooms</code> &#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>Int?</code> &#x800C;&#x4E0D;&#x662F; <code>Int</code>&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x4E2A; <code>Residence</code> &#x7684;&#x5B9E;&#x4F8B;&#x8D4B;&#x7ED9; <code>john.residence</code>&#xFF0C;&#x8FD9;&#x6837;&#x5B83;&#x5C31;&#x4E0D;&#x518D;&#x662F; <code>nil</code> &#x4E86;&#xFF1A;</p>
<pre><code class="lang-swift">john.residence = <span class="hljs-type">Residence</span>()
</code></pre>
<p><code>john.residence</code> &#x73B0;&#x5728;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x5B9E;&#x9645;&#x7684; <code>Residence</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x800C;&#x4E0D;&#x518D;&#x662F; <code>nil</code>&#x3002;&#x5982;&#x679C;&#x4F60;&#x8BD5;&#x56FE;&#x4F7F;&#x7528;&#x5148;&#x524D;&#x7684;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE; <code>numberOfRooms</code>&#xFF0C;&#x5B83;&#x73B0;&#x5728;&#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x4E3A; <code>1</code> &#x7684; <code>Int?</code> &#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> roomCount = john.residence?.numberOfRooms {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s residence has <span class="hljs-subst">\(roomCount)</span> room(s).&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the number of rooms.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;John&apos;s residence has 1 room(s).&#x201D;</span>
</code></pre>
<h2 id="defining-model-classes-for-optional-chaining">&#x4E3A;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B; </h2>
<p>&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x591A;&#x5C42;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x4E0B;&#x6807;&#x3002;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x5728;&#x590D;&#x6742;&#x7684;&#x6A21;&#x578B;&#x4E2D;&#x5411;&#x4E0B;&#x8BBF;&#x95EE;&#x5404;&#x79CD;&#x5B50;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x5224;&#x65AD;&#x80FD;&#x5426;&#x8BBF;&#x95EE;&#x5B50;&#x5C5E;&#x6027;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x4E0B;&#x6807;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5B9A;&#x4E49;&#x4E86;&#x56DB;&#x4E2A;&#x6A21;&#x578B;&#x7C7B;&#xFF0C;&#x8FD9;&#x4E9B;&#x4F8B;&#x5B50;&#x5305;&#x62EC;&#x591A;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x3002;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x8BF4;&#x660E;&#xFF0C;&#x5728; <code>Person</code> &#x548C; <code>Residence</code> &#x7684;&#x57FA;&#x7840;&#x4E0A;&#x589E;&#x52A0;&#x4E86; <code>Room</code> &#x7C7B;&#x548C; <code>Address</code> &#x7C7B;&#xFF0C;&#x4EE5;&#x53CA;&#x76F8;&#x5173;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x4EE5;&#x53CA;&#x4E0B;&#x6807;&#x3002;</p>
<p><code>Person</code> &#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x57FA;&#x672C;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">var</span> residence: <span class="hljs-type">Residence</span>?
}
</code></pre>
<p><code>Residence</code> &#x7C7B;&#x6BD4;&#x4E4B;&#x524D;&#x590D;&#x6742;&#x4E9B;&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <code>rooms</code> &#x7684;&#x53D8;&#x91CF;&#x5C5E;&#x6027;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x4E3A; <code>[Room]</code> &#x7C7B;&#x578B;&#x7684;&#x7A7A;&#x6570;&#x7EC4;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Residence</span> </span>{
    <span class="hljs-keyword">var</span> rooms = [<span class="hljs-type">Room</span>]()
    <span class="hljs-keyword">var</span> numberOfRooms: <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">return</span> rooms.<span class="hljs-built_in">count</span>
    }
    <span class="hljs-keyword">subscript</span>(i: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Room</span> {
        <span class="hljs-keyword">get</span> {
            <span class="hljs-keyword">return</span> rooms[i]
        }
        <span class="hljs-keyword">set</span> {
            rooms[i] = newValue
        }
    }
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printNumberOfRooms</span><span class="hljs-params">()</span></span> {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The number of rooms is <span class="hljs-subst">\(numberOfRooms)</span>&quot;</span>)
    }
    <span class="hljs-keyword">var</span> address: <span class="hljs-type">Address</span>?
}
</code></pre>
<p>&#x73B0;&#x5728; <code>Residence</code> &#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x5B58;&#x50A8; <code>Room</code> &#x5B9E;&#x4F8B;&#x7684;&#x6570;&#x7EC4;&#xFF0C;<code>numberOfRooms</code> &#x5C5E;&#x6027;&#x88AB;&#x5B9E;&#x73B0;&#x4E3A;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B58;&#x50A8;&#x578B;&#x5C5E;&#x6027;&#x3002;<code>numberOfRooms</code> &#x5C5E;&#x6027;&#x7B80;&#x5355;&#x5730;&#x8FD4;&#x56DE; <code>rooms</code> &#x6570;&#x7EC4;&#x7684; <code>count</code> &#x5C5E;&#x6027;&#x7684;&#x503C;&#x3002;</p>
<p><code>Residence</code> &#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x8BBF;&#x95EE; <code>rooms</code> &#x6570;&#x7EC4;&#x7684;&#x5FEB;&#x6377;&#x65B9;&#x5F0F;&#xFF0C;&#x5373;&#x63D0;&#x4F9B;&#x53EF;&#x8BFB;&#x5199;&#x7684;&#x4E0B;&#x6807;&#x6765;&#x8BBF;&#x95EE; <code>rooms</code> &#x6570;&#x7EC4;&#x4E2D;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5143;&#x7D20;&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;<code>Residence</code> &#x8FD8;&#x63D0;&#x4F9B;&#x4E86; <code>printNumberOfRooms</code> &#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x6253;&#x5370; <code>numberOfRooms</code> &#x7684;&#x503C;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;<code>Residence</code> &#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027; <code>address</code>&#xFF0C;&#x5176;&#x7C7B;&#x578B;&#x4E3A; <code>Address?</code>&#x3002;<code>Address</code> &#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x5728;&#x4E0B;&#x9762;&#x4F1A;&#x8BF4;&#x660E;&#x3002;</p>
<p><code>Room</code> &#x7C7B;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7C7B;&#xFF0C;&#x5176;&#x5B9E;&#x4F8B;&#x88AB;&#x5B58;&#x50A8;&#x5728; <code>rooms</code> &#x6570;&#x7EC4;&#x4E2D;&#x3002;&#x8BE5;&#x7C7B;&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x5C5E;&#x6027; <code>name</code>&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x5C06;&#x8BE5;&#x5C5E;&#x6027;&#x8BBE;&#x7F6E;&#x4E3A;&#x9002;&#x5F53;&#x7684;&#x623F;&#x95F4;&#x540D;&#x7684;&#x521D;&#x59CB;&#x5316;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Room</span> </span>{
    <span class="hljs-keyword">let</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(name: <span class="hljs-type">String</span>) { <span class="hljs-keyword">self</span>.name = name }
}
</code></pre>
<p>&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7C7B;&#x662F; <code>Address</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x7C7B;&#x6709;&#x4E09;&#x4E2A; <code>String?</code> &#x7C7B;&#x578B;&#x7684;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x3002;<code>buildingName</code> &#x4EE5;&#x53CA; <code>buildingNumber</code> &#x5C5E;&#x6027;&#x5206;&#x522B;&#x8868;&#x793A;&#x5927;&#x53A6;&#x7684;&#x540D;&#x79F0;&#x548C;&#x53F7;&#x7801;&#xFF0C;&#x7B2C;&#x4E09;&#x4E2A;&#x5C5E;&#x6027; <code>street</code> &#x8868;&#x793A;&#x5927;&#x53A6;&#x6240;&#x5728;&#x8857;&#x9053;&#x7684;&#x540D;&#x79F0;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Address</span> </span>{
    <span class="hljs-keyword">var</span> buildingName: <span class="hljs-type">String</span>?
    <span class="hljs-keyword">var</span> buildingNumber: <span class="hljs-type">String</span>?
    <span class="hljs-keyword">var</span> street: <span class="hljs-type">String</span>?
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">buildingIdentifier</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span>? {
        <span class="hljs-keyword">if</span> buildingName != <span class="hljs-literal">nil</span> {
            <span class="hljs-keyword">return</span> buildingName
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> buildingNumber = buildingNumber, <span class="hljs-keyword">let</span> street = street {
            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;<span class="hljs-subst">\(buildingNumber)</span> <span class="hljs-subst">\(street)</span>&quot;</span>
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
        }
    }
}
</code></pre>
<p><code>Address</code> &#x7C7B;&#x63D0;&#x4F9B;&#x4E86; <code>buildingIdentifier()</code> &#x65B9;&#x6CD5;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x4E3A; <code>String?</code>&#x3002; &#x5982;&#x679C; <code>buildingName</code> &#x6709;&#x503C;&#x5219;&#x8FD4;&#x56DE; <code>buildingName</code>&#x3002;&#x6216;&#x8005;&#xFF0C;&#x5982;&#x679C; <code>buildingNumber</code> &#x548C; <code>street</code> &#x5747;&#x6709;&#x503C;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x4E24;&#x8005;&#x62FC;&#x63A5;&#x5F97;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;</p>
<h2 id="accessing-properties-through-optional-chaining">&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x5C5E;&#x6027; </h2>
<p>&#x6B63;&#x5982; <a href="#optional_chaining_as_an_alternative_to_forced_unwrapping">&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4EE3;&#x66FF;&#x5F3A;&#x5236;&#x5C55;&#x5F00;</a> &#x4E2D;&#x6240;&#x8FF0;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5728;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x5224;&#x65AD;&#x8BBF;&#x95EE;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x524D;&#x9762;&#x5B9A;&#x4E49;&#x8FC7;&#x7684;&#x7C7B;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>Person</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x7136;&#x540E;&#x50CF;&#x4E4B;&#x524D;&#x4E00;&#x6837;&#xFF0C;&#x5C1D;&#x8BD5;&#x8BBF;&#x95EE; <code>numberOfRooms</code> &#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> john = <span class="hljs-type">Person</span>()
<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> roomCount = john.residence?.numberOfRooms {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s residence has <span class="hljs-subst">\(roomCount)</span> room(s).&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the number of rooms.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Unable to retrieve the number of rooms.&#x201D;</span>
</code></pre>
<p>&#x56E0;&#x4E3A; <code>john.residence</code> &#x4E3A; <code>nil</code>&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x4E2A;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4F9D;&#x65E7;&#x4F1A;&#x50CF;&#x5148;&#x524D;&#x4E00;&#x6837;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8BBE;&#x7F6E;&#x5C5E;&#x6027;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> someAddress = <span class="hljs-type">Address</span>()
someAddress.buildingNumber = <span class="hljs-string">&quot;29&quot;</span>
someAddress.street = <span class="hljs-string">&quot;Acacia Road&quot;</span>
john.residence?.address = someAddress
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7; <code>john.residence</code> &#x6765;&#x8BBE;&#x5B9A; <code>address</code> &#x5C5E;&#x6027;&#x4E5F;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x56E0;&#x4E3A; <code>john.residence</code> &#x5F53;&#x524D;&#x4E3A; <code>nil</code>&#x3002;</p>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x8D4B;&#x503C;&#x8FC7;&#x7A0B;&#x662F;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5931;&#x8D25;&#x65F6;&#xFF0C;&#x7B49;&#x53F7;&#x53F3;&#x4FA7;&#x7684;&#x4EE3;&#x7801;&#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#x3002;&#x5BF9;&#x4E8E;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x6765;&#x8BF4;&#xFF0C;&#x5F88;&#x96BE;&#x9A8C;&#x8BC1;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x50CF;&#x8FD9;&#x6837;&#x8D4B;&#x503C;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x526F;&#x4F5C;&#x7528;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5B8C;&#x6210;&#x4E86;&#x540C;&#x6837;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA; <code>Address</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x8BE5;&#x5B9E;&#x4F8B;&#x8FD4;&#x56DE;&#x7528;&#x4E8E;&#x8D4B;&#x503C;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x4F1A;&#x5728;&#x8FD4;&#x56DE;&#x524D;&#x6253;&#x5370;&#x201C;Function was called&#x201D;&#xFF0C;&#x8FD9;&#x4F7F;&#x4F60;&#x80FD;&#x9A8C;&#x8BC1;&#x7B49;&#x53F7;&#x53F3;&#x4FA7;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x5426;&#x88AB;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">createAddress</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">Address</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Function was called.&quot;</span>)

    <span class="hljs-keyword">let</span> someAddress = <span class="hljs-type">Address</span>()
    someAddress.buildingNumber = <span class="hljs-string">&quot;29&quot;</span>
    someAddress.street = <span class="hljs-string">&quot;Acacia Road&quot;</span>

    <span class="hljs-keyword">return</span> someAddress
}
john.residence?.address = createAddress()
</code></pre>
<p>&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x6253;&#x5370;&#x6D88;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x51FA; <code>createAddress()</code> &#x51FD;&#x6570;&#x5E76;&#x672A;&#x88AB;&#x6267;&#x884C;&#x3002;</p>
<h2 id="calling-methods-through-optional-chaining">&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528;&#x65B9;&#x6CD5; </h2>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x5224;&#x65AD;&#x662F;&#x5426;&#x8C03;&#x7528;&#x6210;&#x529F;&#xFF0C;&#x5373;&#x4F7F;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<p><code>Residence</code> &#x7C7B;&#x4E2D;&#x7684; <code>printNumberOfRooms()</code> &#x65B9;&#x6CD5;&#x6253;&#x5370;&#x5F53;&#x524D;&#x7684; <code>numberOfRooms</code> &#x503C;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printNumberOfRooms</span><span class="hljs-params">()</span></span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The number of rooms is <span class="hljs-subst">\(numberOfRooms)</span>&quot;</span>)
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#x5177;&#x6709;&#x9690;&#x5F0F;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B; <code>Void</code>&#xFF0C;&#x5982; <a href="06_Functions.html#functions_without_return_values">&#x65E0;&#x8FD4;&#x56DE;&#x503C;&#x51FD;&#x6570;</a> &#x4E2D;&#x6240;&#x8FF0;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#x4E5F;&#x4F1A;&#x8FD4;&#x56DE; <code>()</code>&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x7A7A;&#x7684;&#x5143;&#x7EC4;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5728;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x4F1A;&#x662F; <code>Void?</code>&#xFF0C;&#x800C;&#x4E0D;&#x662F; <code>Void</code>&#xFF0C;&#x56E0;&#x4E3A;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5F97;&#x5230;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x90FD;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>if</code> &#x8BED;&#x53E5;&#x6765;&#x5224;&#x65AD;&#x80FD;&#x5426;&#x6210;&#x529F;&#x8C03;&#x7528; <code>printNumberOfRooms()</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5373;&#x4F7F;&#x65B9;&#x6CD5;&#x672C;&#x8EAB;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x901A;&#x8FC7;&#x5224;&#x65AD;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5426;&#x4E3A; <code>nil</code> &#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x8C03;&#x7528;&#x662F;&#x5426;&#x6210;&#x529F;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> john.residence?.printNumberOfRooms() != <span class="hljs-literal">nil</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;It was possible to print the number of rooms.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;It was not possible to print the number of rooms.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;It was not possible to print the number of rooms.&#x201D;</span>
</code></pre>
<p>&#x540C;&#x6837;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x636E;&#x6B64;&#x5224;&#x65AD;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4E3A;&#x5C5E;&#x6027;&#x8D4B;&#x503C;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684; <a href="#accessing_properties_through_optional_chaining">&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;</a> &#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C1D;&#x8BD5;&#x7ED9; <code>john.residence</code> &#x4E2D;&#x7684; <code>address</code> &#x5C5E;&#x6027;&#x8D4B;&#x503C;&#xFF0C;&#x5373;&#x4F7F; <code>residence</code> &#x4E3A; <code>nil</code>&#x3002;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7ED9;&#x5C5E;&#x6027;&#x8D4B;&#x503C;&#x4F1A;&#x8FD4;&#x56DE; <code>Void?</code>&#xFF0C;&#x901A;&#x8FC7;&#x5224;&#x65AD;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5426;&#x4E3A; <code>nil</code> &#x5C31;&#x53EF;&#x4EE5;&#x77E5;&#x9053;&#x8D4B;&#x503C;&#x662F;&#x5426;&#x6210;&#x529F;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> (john.residence?.address = someAddress) != <span class="hljs-literal">nil</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;It was possible to set the address.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;It was not possible to set the address.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;It was not possible to set the address.&#x201D;</span>
</code></pre>
<h2 id="accessing-subscripts-through-optional-chaining">&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x4E0B;&#x6807; </h2>
<p>&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x8BBF;&#x95EE;&#x4E0B;&#x6807;&#xFF0C;&#x5E76;&#x4E14;&#x5224;&#x65AD;&#x4E0B;&#x6807;&#x8C03;&#x7528;&#x662F;&#x5426;&#x6210;&#x529F;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x53EF;&#x9009;&#x503C;&#x7684;&#x4E0B;&#x6807;&#x65F6;&#xFF0C;&#x5E94;&#x8BE5;&#x5C06;&#x95EE;&#x53F7;&#x653E;&#x5728;&#x4E0B;&#x6807;&#x65B9;&#x62EC;&#x53F7;&#x7684;&#x524D;&#x9762;&#x800C;&#x4E0D;&#x662F;&#x540E;&#x9762;&#x3002;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x95EE;&#x53F7;&#x4E00;&#x822C;&#x76F4;&#x63A5;&#x8DDF;&#x5728;&#x53EF;&#x9009;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x540E;&#x9762;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x7528;&#x4E0B;&#x6807;&#x8BBF;&#x95EE; <code>john.residence</code> &#x5C5E;&#x6027;&#x5B58;&#x50A8;&#x7684; <code>Residence</code> &#x5B9E;&#x4F8B;&#x7684; <code>rooms</code> &#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x623F;&#x95F4;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x56E0;&#x4E3A; <code>john.residence</code> &#x4E3A; <code>nil</code>&#xFF0C;&#x6240;&#x4EE5;&#x4E0B;&#x6807;&#x8C03;&#x7528;&#x5931;&#x8D25;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> firstRoomName = john.residence?[<span class="hljs-number">0</span>].name {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The first room name is <span class="hljs-subst">\(firstRoomName)</span>.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the first room name.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Unable to retrieve the first room name.&#x201D;</span>
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x95EE;&#x53F7;&#x76F4;&#x63A5;&#x653E;&#x5728; <code>john.residence</code> &#x7684;&#x540E;&#x9762;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x65B9;&#x62EC;&#x53F7;&#x7684;&#x524D;&#x9762;&#xFF0C;&#x56E0;&#x4E3A; <code>john.residence</code> &#x662F;&#x53EF;&#x9009;&#x503C;&#x3002;</p>
<p>&#x7C7B;&#x4F3C;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x6807;&#xFF0C;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8D4B;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift">john.residence?[<span class="hljs-number">0</span>] = <span class="hljs-type">Room</span>(name: <span class="hljs-string">&quot;Bathroom&quot;</span>)
</code></pre>
<p>&#x8FD9;&#x6B21;&#x8D4B;&#x503C;&#x540C;&#x6837;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x56E0;&#x4E3A; <code>residence</code> &#x76EE;&#x524D;&#x662F; <code>nil</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>Residence</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x4E3A;&#x5176; <code>rooms</code> &#x6570;&#x7EC4;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B; <code>Room</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x7136;&#x540E;&#x5C06; <code>Residence</code> &#x5B9E;&#x4F8B;&#x8D4B;&#x503C;&#x7ED9; <code>john.residence</code>&#xFF0C;&#x90A3;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x548C;&#x4E0B;&#x6807;&#x6765;&#x8BBF;&#x95EE;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> johnsHouse = <span class="hljs-type">Residence</span>()
johnsHouse.rooms.append(<span class="hljs-type">Room</span>(name: <span class="hljs-string">&quot;Living Room&quot;</span>))
johnsHouse.rooms.append(<span class="hljs-type">Room</span>(name: <span class="hljs-string">&quot;Kitchen&quot;</span>))
john.residence = johnsHouse

<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> firstRoomName = john.residence?[<span class="hljs-number">0</span>].name {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The first room name is <span class="hljs-subst">\(firstRoomName)</span>.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the first room name.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;The first room name is Living Room.&#x201D;</span>
</code></pre>
<h3 id="accessing-subscripts-of-optional-type">&#x8BBF;&#x95EE;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684;&#x4E0B;&#x6807; </h3>
<p>&#x5982;&#x679C;&#x4E0B;&#x6807;&#x8FD4;&#x56DE;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x503C;&#xFF0C;&#x6BD4;&#x5982; Swift &#x4E2D; <code>Dictionary</code> &#x7C7B;&#x578B;&#x7684;&#x952E;&#x7684;&#x4E0B;&#x6807;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4E0B;&#x6807;&#x7684;&#x7ED3;&#x5C3E;&#x62EC;&#x53F7;&#x540E;&#x9762;&#x653E;&#x4E00;&#x4E2A;&#x95EE;&#x53F7;&#x6765;&#x5728;&#x5176;&#x53EF;&#x9009;&#x8FD4;&#x56DE;&#x503C;&#x4E0A;&#x8FDB;&#x884C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> testScores = [<span class="hljs-string">&quot;Dave&quot;</span>: [<span class="hljs-number">86</span>, <span class="hljs-number">82</span>, <span class="hljs-number">84</span>], <span class="hljs-string">&quot;Bev&quot;</span>: [<span class="hljs-number">79</span>, <span class="hljs-number">94</span>, <span class="hljs-number">81</span>]]
testScores[<span class="hljs-string">&quot;Dave&quot;</span>]?[<span class="hljs-number">0</span>] = <span class="hljs-number">91</span>
testScores[<span class="hljs-string">&quot;Bev&quot;</span>]?[<span class="hljs-number">0</span>] += <span class="hljs-number">1</span>
testScores[<span class="hljs-string">&quot;Brian&quot;</span>]?[<span class="hljs-number">0</span>] = <span class="hljs-number">72</span>
<span class="hljs-comment">// &quot;Dave&quot; &#x6570;&#x7EC4;&#x73B0;&#x5728;&#x662F; [91, 82, 84]&#xFF0C;&quot;Bev&quot; &#x6570;&#x7EC4;&#x73B0;&#x5728;&#x662F; [80, 94, 81]</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A; <code>testScores</code> &#x6570;&#x7EC4;&#xFF0C;&#x5305;&#x542B;&#x4E86;&#x4E24;&#x4E2A;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x5206;&#x522B;&#x628A; <code>String</code> &#x7C7B;&#x578B;&#x7684;&#x952E;&#x6620;&#x5C04;&#x5230;&#x4E00;&#x4E2A; <code>Int</code> &#x503C;&#x7684;&#x6570;&#x7EC4;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x628A; <code>&quot;Dave&quot;</code> &#x6570;&#x7EC4;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x8BBE;&#x4E3A; <code>91</code>&#xFF0C;&#x628A; <code>&quot;Bev&quot;</code> &#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20; <code>+1</code>&#xFF0C;&#x7136;&#x540E;&#x5C1D;&#x8BD5;&#x628A; <code>&quot;Brian&quot;</code> &#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x8BBE;&#x4E3A; <code>72</code>&#x3002;&#x524D;&#x4E24;&#x4E2A;&#x8C03;&#x7528;&#x6210;&#x529F;&#xFF0C;&#x56E0;&#x4E3A; <code>testScores</code> &#x5B57;&#x5178;&#x4E2D;&#x5305;&#x542B; <code>&quot;Dave&quot;</code> &#x548C; <code>&quot;Bev&quot;</code> &#x8FD9;&#x4E24;&#x4E2A;&#x952E;&#x3002;&#x4F46;&#x662F; <code>testScores</code> &#x5B57;&#x5178;&#x4E2D;&#x6CA1;&#x6709; <code>&quot;Brian&quot;</code> &#x8FD9;&#x4E2A;&#x952E;&#xFF0C;&#x6240;&#x4EE5;&#x7B2C;&#x4E09;&#x4E2A;&#x8C03;&#x7528;&#x5931;&#x8D25;&#x3002;</p>
<h2 id="linking-multiple-levels-of-chaining">&#x8FDE;&#x63A5;&#x591A;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528; </h2>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x591A;&#x4E2A;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5728;&#x66F4;&#x6DF1;&#x7684;&#x6A21;&#x578B;&#x5C42;&#x7EA7;&#x4E2D;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x4EE5;&#x53CA;&#x4E0B;&#x6807;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x591A;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4E0D;&#x4F1A;&#x589E;&#x52A0;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x53EF;&#x9009;&#x5C42;&#x7EA7;&#x3002;</p>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF1A;</p>
<ul>
<li>&#x5982;&#x679C;&#x4F60;&#x8BBF;&#x95EE;&#x7684;&#x503C;&#x4E0D;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;&#x53EF;&#x9009;&#x503C;&#x3002;</li>
<li>&#x5982;&#x679C;&#x4F60;&#x8BBF;&#x95EE;&#x7684;&#x503C;&#x5C31;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x4E0D;&#x4F1A;&#x8BA9;&#x53EF;&#x9009;&#x8FD4;&#x56DE;&#x503C;&#x53D8;&#x5F97;&#x201C;&#x66F4;&#x53EF;&#x9009;&#x201D;&#x3002;</li>
</ul>
<p>&#x56E0;&#x6B64;&#xFF1A;</p>
<ul>
<li>&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A; <code>Int</code> &#x503C;&#xFF0C;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE; <code>Int?</code>&#xFF0C;&#x65E0;&#x8BBA;&#x4F7F;&#x7528;&#x4E86;&#x591A;&#x5C11;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x3002;</li>
<li>&#x7C7B;&#x4F3C;&#x7684;&#xFF0C;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x8BBF;&#x95EE; <code>Int?</code> &#x503C;&#xFF0C;&#x4F9D;&#x65E7;&#x4F1A;&#x8FD4;&#x56DE; <code>Int?</code> &#x503C;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x8FD4;&#x56DE; <code>Int??</code>&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C1D;&#x8BD5;&#x8BBF;&#x95EE; <code>john</code> &#x4E2D;&#x7684; <code>residence</code> &#x5C5E;&#x6027;&#x4E2D;&#x7684; <code>address</code> &#x5C5E;&#x6027;&#x4E2D;&#x7684; <code>street</code> &#x5C5E;&#x6027;&#x3002;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E86;&#x4E24;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;<code>residence</code> &#x4EE5;&#x53CA; <code>address</code> &#x90FD;&#x662F;&#x53EF;&#x9009;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> johnsStreet = john.residence?.address?.street {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s street name is <span class="hljs-subst">\(johnsStreet)</span>.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the address.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Unable to retrieve the address.&#x201D;</span>
</code></pre>
<p><code>john.residence</code> &#x73B0;&#x5728;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684; <code>Residence</code> &#x5B9E;&#x4F8B;&#x3002;&#x7136;&#x800C;&#xFF0C;<code>john.residence.address</code> &#x7684;&#x503C;&#x5F53;&#x524D;&#x4E3A; <code>nil</code>&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x8C03;&#x7528; <code>john.residence?.address?.street</code> &#x4F1A;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>street</code> &#x7684;&#x5C5E;&#x6027;&#x4E3A; <code>String?</code>&#x3002;<code>john.residence?.address?.street</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4E5F;&#x4F9D;&#x7136;&#x662F; <code>String?</code>&#xFF0C;&#x5373;&#x4F7F;&#x5DF2;&#x7ECF;&#x4F7F;&#x7528;&#x4E86;&#x4E24;&#x5C42;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E3A; <code>john.residence.address</code> &#x8D4B;&#x503C;&#x4E00;&#x4E2A; <code>Address</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x4E14;&#x4E3A; <code>address</code> &#x4E2D;&#x7684; <code>street</code> &#x5C5E;&#x6027;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x503C;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x80FD;&#x8FC7;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8BBF;&#x95EE; <code>street</code> &#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> johnsAddress = <span class="hljs-type">Address</span>()
johnsAddress.buildingName = <span class="hljs-string">&quot;The Larches&quot;</span>
johnsAddress.street = <span class="hljs-string">&quot;Laurel Street&quot;</span>
john.residence?.address = johnsAddress

<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> johnsStreet = john.residence?.address?.street {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s street name is <span class="hljs-subst">\(johnsStreet)</span>.&quot;</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Unable to retrieve the address.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;John&apos;s street name is Laurel Street.&#x201D;</span>
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x56E0;&#x4E3A; <code>john.residence</code> &#x5305;&#x542B;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684; <code>Address</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x6240;&#x4EE5;&#x5BF9; <code>john.residence</code> &#x7684; <code>address</code> &#x5C5E;&#x6027;&#x8D4B;&#x503C;&#x5C06;&#x4F1A;&#x6210;&#x529F;&#x3002;</p>
<h2 id="chaining-on-methods-with-optional-return-values">&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x9009;&#x8FD4;&#x56DE;&#x503C;&#x4E0A;&#x8FDB;&#x884C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528; </h2>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x5982;&#x4F55;&#x5728;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x83B7;&#x53D6;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x503C;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x503C;&#x4E0A;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9700;&#x8981;&#x7EE7;&#x7EED;&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x9009;&#x8FD4;&#x56DE;&#x503C;&#x4E0A;&#x8FDB;&#x884C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528; <code>Address</code> &#x7684; <code>buildingIdentifier()</code> &#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE; <code>String?</code> &#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;&#x5982;&#x4E0A;&#x6240;&#x8FF0;&#xFF0C;&#x901A;&#x8FC7;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x6765;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F9D;&#x65E7;&#x4F1A;&#x662F; <code>String?</code> &#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> buildingIdentifier = john.residence?.address?.buildingIdentifier() {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s building identifier is <span class="hljs-subst">\(buildingIdentifier)</span>.&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;John&apos;s building identifier is The Larches.&#x201D;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x5728;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4E0A;&#x8FDB;&#x884C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x540E;&#x9762;&#x52A0;&#x4E0A;&#x95EE;&#x53F7;&#x5373;&#x53EF;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> beginsWithThe =
    john.residence?.address?.buildingIdentifier()?.hasPrefix(<span class="hljs-string">&quot;The&quot;</span>) {
        <span class="hljs-keyword">if</span> beginsWithThe {
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s building identifier begins with \&quot;The\&quot;.&quot;</span>)
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;John&apos;s building identifier does not begin with \&quot;The\&quot;.&quot;</span>)
        }
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;John&apos;s building identifier begins with &quot;The&quot;.&#x201D;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x540E;&#x9762;&#x52A0;&#x4E0A;&#x95EE;&#x53F7;&#x662F;&#x56E0;&#x4E3A;&#x4F60;&#x8981;&#x5728; <code>buildingIdentifier()</code> &#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x9009;&#x8FD4;&#x56DE;&#x503C;&#x4E0A;&#x8FDB;&#x884C;&#x53EF;&#x9009;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;&#x800C;&#x4E0D;&#x662F; <code>buildingIdentifier()</code> &#x65B9;&#x6CD5;&#x672C;&#x8EAB;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="15_Deinitialization.html" class="navigation navigation-prev " aria-label="Previous page: 析构过程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="17_Error_Handling.html" class="navigation navigation-next " aria-label="Next page: 错误处理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"可选链","level":"1.3.16","depth":2,"next":{"title":"错误处理","level":"1.3.17","depth":2,"path":"chapter2/17_Error_Handling.md","ref":"chapter2/17_Error_Handling.md","articles":[]},"previous":{"title":"析构过程","level":"1.3.15","depth":2,"path":"chapter2/15_Deinitialization.md","ref":"chapter2/15_Deinitialization.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/16_Optional_Chaining.md","mtime":"2019-07-05T01:01:08.088Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

